package com.renli.service.impl;

import com.renli.bean.ResultJson;
import com.renli.mapper.PersonnelManagementMapper;
import com.renli.mapper.RecruitMapper;
import com.renli.pojo.*;
import com.renli.service.PersonnelManagementService;
import com.renli.vo.ArchivesVo;
import com.renli.vo.InterviewVo;
import com.renli.vo.SelectArchives;
import com.renli.vo.SelectChangeStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2020/09/09 10:13
 * Description:
 * Version: V1.0
 */
@Service
public class PersonnelManagementServiceImpl implements PersonnelManagementService {
    @Autowired
    private PersonnelManagementMapper personnelManagementMapper;
    @Autowired
    RecruitMapper recruitMapper;

    @Override
    public List<Archives> query_person(int id, int state, int state1, String createTime) {
        return personnelManagementMapper.query_person(id, state, state1, createTime);
    }

    @Override
    public ResultJson insertArchives(BackupArhives backupArhives, ChangeStatistics changeStatistics) {
        ResultJson resultJson=new ResultJson();
        Event event=new Event();
        event.setEventTypeId(8);
        event.setName(backupArhives.getBackrealName()+"：档案新增");
     if (personnelManagementMapper.insertArchives(backupArhives)>0&&recruitMapper.insertEvent(event)>0) {
         changeStatistics.setBackupArhivesId(backupArhives.getId());
         changeStatistics.setChangeTypeId(1);
         changeStatistics.setEventId(event.getId());
        if (personnelManagementMapper.insertchangeStatistics(changeStatistics)>0){
            return resultJson.setSuccess("成功");
        }
     }
        return resultJson.setFail("失败");
    }

    @Override
    public ResultJson SubmitForReview(Map map) {
        ResultJson resultJson=new ResultJson();
        ChangeStatistics changeStatistics=new ChangeStatistics();
        BackupArhives backupArhives =new BackupArhives();
        Event event=new Event();
        event.setEventTypeId(8);
        event.setName(map.get("realName")+"：档案新增");
        if(recruitMapper.insertEvent(event)>0){
            changeStatistics.setEventId(event.getId());
            changeStatistics.setId((Integer) map.get("changeStatisticsId"));

            backupArhives.setId((Integer)map.get("backupArhivesId"));
            backupArhives.setBackupArhivesState(1);
            if(recruitMapper.updateChangeStatistics(changeStatistics)>0&&recruitMapper.updateBackupArhives(backupArhives)>0){
                return resultJson.setSuccess("成功");
            }
        } return resultJson.setFail("失败");
    }


    @Override
    public ResultJson selectResumeNotUse() {
        ResultJson resultJson=new ResultJson();
        List<Resume> resumes=personnelManagementMapper.selectResumeNotUse();
        return resultJson.setSuccess("遍历成功",resumes);
    }



    @Override
    public ResultJson delectArchives(Archives archives) {
        ResultJson resultJson=new ResultJson();
        archives.setState(-1);
        if(personnelManagementMapper.delectArchives(archives)>0){
            return resultJson.setSuccess("删除成功");}
        return resultJson.setFail("删除失败");
    }

    @Override
    public ResultJson selectArchives(SelectArchives selectArchives) {
        ResultJson resultJson=new ResultJson();
       List<ArchivesVo> list=personnelManagementMapper.selectArchives(selectArchives);
        return resultJson.setSuccess("成功",list);
    }

    @Override
    public ResultJson selectchangeType() {
        ResultJson resultJson=new ResultJson();
        List<ChangeType> list=personnelManagementMapper.selectchangeType();
        return resultJson.setSuccess("成功",list);
    }



    @Override
    public ResultJson alternationsArchivesAudit(Archives archives, BackupArhives backupArhives, ChangeType changeType,User user) {
        ResultJson resultJson=new ResultJson();
        Event event=new Event();
        SelectArchives selectArchives=new SelectArchives();
        selectArchives.setId(archives.getId());
        List<ArchivesVo> archivesVos = personnelManagementMapper.selectArchives(selectArchives);
        String realName = archivesVos.get(0).getRealName();
        ChangeStatistics changeStatistics = new ChangeStatistics();
        if(personnelManagementMapper.insertBackupArhives(backupArhives)>0){
            event.setName(realName+":"+changeType.getName());
            event.setEventTypeId(7);
        if (   recruitMapper.insertEvent(event)>0){

            Integer backupArhivesId = backupArhives.getId();
            changeStatistics.setBackupArhivesId(backupArhivesId);
            changeStatistics.setChangeTypeId(changeType.getId());
            changeStatistics.setBeforeArchivesId(archives.getId());
            changeStatistics.setReguserId(user.getId());
            changeStatistics.setEventId(event.getId());
            if (personnelManagementMapper.insertchangeStatistics(changeStatistics)>0){
                return resultJson.setSuccess("提交成功,等待审核");
            }
        }}
        return resultJson.setFail("提交失败");
    }

    @Override
    public ResultJson insertArchivesReview(Event event) {
        ResultJson resultJson=new ResultJson();
        InterviewVo interviewVo=new InterviewVo();
        interviewVo.setEventState(event.getState());
        interviewVo.setUserId(event.getUserId());
        if (recruitMapper.updateEvent(interviewVo)>0){
            return resultJson.setSuccess("成功");
        }else {
            return resultJson.setFail("失败");
        }
    }

    @Override
    public ResultJson selectChangeStatistics(SelectChangeStatisticsVo selectChangeStatisticsVo) {
        ResultJson resultJson=new ResultJson();
        List<Map<String, Object>> map = personnelManagementMapper.selectChangeStatistics(selectChangeStatisticsVo);
       return resultJson.setSuccess("成功",map);
    }

    @Override
    public ResultJson updateChangeStatisticsReview(ChangeStatistics changeStatistics, Event event) {
        ResultJson resultJson=new ResultJson();
        InterviewVo interviewVo=new InterviewVo();
        interviewVo.setEventState(event.getState());
        interviewVo.setUserId(event.getUserId());
        if (event.getState()==-1){
            if (recruitMapper.updateEvent(interviewVo)>0){
                return resultJson.setSuccess("成功,已驳回");
            }else {
                return resultJson.setFail("失败");
            }
        }else{

            Integer backupArhivesId = changeStatistics.getBackupArhivesId();
            BackupArhives backupArhives = personnelManagementMapper.selectBackupArchives(backupArhivesId);
            if (recruitMapper.updateEvent(interviewVo)>0&&personnelManagementMapper.updateArchives(backupArhives)>0){
                return resultJson.setSuccess("成功,已变更");
            }else
            {
                return resultJson.setFail("失败");
            }

        }
    }

    @Override
    public ResultJson selectbackupArhives(SelectArchives selectArchives) {
        ResultJson resultJson=new ResultJson();
        List<ArchivesVo> list=personnelManagementMapper.selectbackupArhives(selectArchives);
        return resultJson.setSuccess("成功",list);
    }

    @Override
    public ResultJson insertArchivesSave(BackupArhives backupArhives, ChangeStatistics changeStatistics) {
        ResultJson resultJson=new ResultJson();
        backupArhives.setBackupArhivesState(0);
        if (personnelManagementMapper.insertArchives(backupArhives)>0) {
            changeStatistics.setBackupArhivesId(backupArhives.getId());
            changeStatistics.setChangeTypeId(1);
            if (personnelManagementMapper.insertchangeStatistics(changeStatistics)>0){
                return resultJson.setSuccess("保存成功");
            }
        }
        return resultJson.setFail("失败");
    }



}
